#pragma once

#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class InetAddr
{
private:
    void getAddress(std::string* ip, uint16_t* port)
    {
        *port = ntohs(_addr.sin_port);
        *ip = inet_ntoa(_addr.sin_addr);    //4字节IP转点分十进制IP
    }
public:
    InetAddr(){}
    
    InetAddr(struct sockaddr_in addr)
    : _addr(addr)
    {
        getAddress(&_ip, &_port);
    }

    InetAddr(uint16_t port, std::string ip)
    : _ip(ip), _port(port)
    {
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(_port);
        _addr.sin_addr.s_addr = inet_addr(_ip.c_str());
    }

    std::string IP()
    {
        return _ip;
    }

    uint16_t Port()
    {
        return _port;
    }

    struct sockaddr_in Addr()
    {
        return _addr;
    }

    bool operator == (const InetAddr& addr)
    {
        return addr._ip == _ip && addr._port == _port;
    }
private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};

